package ui.footer 
{
	import com.gaiaframework.api.Gaia;
	import com.greensock.easing.Strong;
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import pages.Pages;
	import ui.footer.FooterMenu;
	import ui.footer.WebSiteTitle;
	import utils.ShapeUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class Footer extends MovieClip 
	{
		private var _calage:Sprite;
		private var _websiteTitle:WebSiteTitle;
		private var _miniMap:MiniMap;
		private var _parent:*;
		public function Footer() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function resize():void 
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			_calage.width = sw;
			
			_calage.x = 0;
			
			
			_miniMap.x = 5;
			
			
			_websiteTitle.x = _miniMap.x + _miniMap.width + 10;
			_websiteTitle.y = 22;
					
			var fw:int = int(_calage.height - _miniMap.height - 5);
			if(_miniMap.y != fw)_miniMap.y = fw;
			this.y = sh - _calage.height;
			TweenMax.to(this, 0, {transformAroundPoint:{point:new Point(0,sh - _calage.height), y:sh - _calage.height}});
		}
		
		public function open():void 
		{
			if (!TweenMax.isTweening(_calage) && _calage.height!=232)
			{
				TweenMax.to(_calage, .5, { height:232,alpha:.8,onUpdate:resize,ease:Strong.easeIn,onComplete:addFooterListener } );
				TweenMax.to(_websiteTitle, .5, { alpha:0 } );
				TweenMax.to(_miniMap, .5, { alpha:1 } );
				
				TweenMax.delayedCall(5, _stageOnMouseMove );
			}
		}
		
		private function addFooterListener():void 
		{
			this.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
		}
		
		private function _mouseMove(e:MouseEvent=null):void 
		{
			this.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
			trace("[Footer._stageOnMouseMove] ROLLOVER");
			Gaia.api.getPage(Pages.TIMELINE).content.updateTimeLine('over');
			TweenMax.to(_calage, .5, { height:232, alpha:.8, onUpdate:resize, ease:Strong.easeIn } );
			TweenMax.to(_websiteTitle, .5, { alpha:0 } );
			
			TweenMax.to(_miniMap, .4, { alpha:1,delay:.5 } );
			stage.addEventListener(MouseEvent.MOUSE_MOVE, _stageOnMouseMove);
		}
		
		public function _stageOnMouseMove(e:MouseEvent=null):void 
		{
			if (stage.mouseY < this.y)
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, _stageOnMouseMove);
				
				TweenMax.to(_calage, .5, { height:37, onUpdate:resize,ease:Strong.easeOut } );
				//TweenMax.to(_websiteTitle, .5, { alpha:1 } );
				addFooterListener();
				if (Gaia.api.getPage(Pages.TIMELINE).content != null)
				{
					Gaia.api.getPage(Pages.TIMELINE).content.updateTimeLine('out');
				}
				//trace("[Footer._stageOnMouseMove] ROLLOUT");
				TweenMax.killTweensOf(_miniMap);
				TweenMax.to(_miniMap, .1, { alpha:0 } );
			}
		}
		
		
		public function close():void 
		{
			if (!TweenMax.isTweening(_calage))
			{
				TweenMax.to(_calage, .5, { height:94,alpha:1,onUpdate:resize,ease:Strong.easeOut } );
				TweenMax.to(_websiteTitle, .5, { alpha:1 } );
				TweenMax.to(_miniMap, .5, { alpha:1 } );
				this.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, _stageOnMouseMove);
			}
			this.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			_parent = parent;
			addBg();
			addWebSiteTitle();
			addMiniMap();
		}
		
		
		private function addMiniMap():void 
		{
			_miniMap = new MiniMap();
			_miniMap.buttonMode = true;
			_miniMap.addEventListener(MouseEvent.CLICK, showExplanation);
			addChild(_miniMap);
		}
		
		private function showExplanation(e:MouseEvent=null):void 
		{
			/*if (Gaia.api.getCurrentBranch() == Pages.HOME)
			{*/
				if (Gaia.api.getPage(Pages.NAV).content._explanationMc.visible == false)
					Gaia.api.getPage(Pages.NAV).content._explanationMc.visible = true;
				else
					Gaia.api.getPage(Pages.NAV).content._explanationMc.visible = false;
			//}
		}
		
		private function addWebSiteTitle():void 
		{
			_websiteTitle = new WebSiteTitle("title");
			_websiteTitle.buttonMode = true;
			addChild(_websiteTitle)
			_websiteTitle.addEventListener(MouseEvent.CLICK, showExplanation);
		}
		
		private function addBg():void 
		{
			_calage = ShapeUtil.getSprite(100, 100, 0x221f1f, 1);
			_calage.cacheAsBitmap = true;
			_calage.height = 94;
			addChild(_calage)
		}
		
	}

}